﻿using Model;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.数据分析
{
    public partial class 原料进出明细 : Sunny.UI.UIForm
    {
        public RawMaterial Raw {  get; set; }
        public 原料进出明细()
        {
            InitializeComponent();
            dateEdit2.DateTime = DateTime.Now;
            dateEdit1.DateTime = dateEdit2.DateTime.AddDays(-120);
        }

        private void 原料进出明细_Load(object sender, EventArgs e)
        {
            Query();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Query();
        }
        private void Query()
        {
            if (Raw != null)
            {
                try
                {
                    var sql = $"SELECT dt.dh,dt.rq,dt.djlx,dt.ksmc,iaeorm.* FROM DanjuTable dt  ,ImportAndExportOfRawMaterials iaeorm WHERE dt.dh=iaeorm.DocNum AND dt.rq BETWEEN '{dateEdit1.DateTime.Date}'" +
                    $" AND '{dateEdit2.DateTime.Date.AddDays(1)}'  AND iaeorm.ProductName = '{Raw.ProductName}' and iaeorm.Num='{Raw.Num}' order by dt.rq asc";
                    var dt = Connect.dbcontext.Ado.GetDataTable(sql);
                    if (dt.Rows.Count > 0)
                    {
                        var raws = new List<RawEntryAndExitDetails>();
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dr["djlx"].ToString() == DanjuLeiXing.原料入库单 || dr["djlx"].ToString() == DanjuLeiXing.原料盘盈单)
                            {
                                raws.Add(new RawEntryAndExitDetails()
                                {
                                    dh = dr["dh"].ToString(),
                                    djlx = dr["djlx"].ToString(),
                                    rq = dr["rq"].ObjToDate(),
                                    ksmc = dr["ksmc"].ToString(),
                                    Num = dr["Num"].ToString(),
                                    ProductName = dr["ProductName"].ToString(),
                                    Specifications = dr["Specifications"].ToString(),
                                    Entry = dr["InventoryQuantity"].ObjToDecimal(),
                                    Unit = dr["Unit"].ToString(),
                                    EntrySecond = dr["InventoryQuantity1"].ObjToDecimal()
                                });
                            }
                            else
                            {
                                raws.Add(new RawEntryAndExitDetails()
                                {
                                    dh = dr["dh"].ToString(),
                                    djlx = dr["djlx"].ToString(),
                                    rq = dr["rq"].ObjToDate(),
                                    ksmc = dr["ksmc"].ToString(),
                                    Num = dr["Num"].ToString(),
                                    ProductName = dr["ProductName"].ToString(),
                                    Specifications = dr["Specifications"].ToString(),
                                    Exit = dr["InventoryQuantity"].ObjToDecimal(),
                                    Unit1 = dr["Unit"].ToString(),
                                    ExitSecond = dr["InventoryQuantity1"].ObjToDecimal()
                                });
                            }
                        }
                        gridControl1.DataSource = raws;
                    }
                }
                catch (Exception ex)
                {
                    ShowMessageBox.ShowErrorBox(ex.Message);
                }
            }
        }
    }
}
